package com.icez.in_app_notication_library

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.view.MotionEvent
import android.view.VelocityTracker
import android.view.View
import android.view.ViewConfiguration
import androidx.core.animation.addListener
import java.lang.Math.abs
import java.lang.Math.max
import kotlin.math.min

internal open class InAppNoticationDismissTouchListener(val mView: View,val mInAppNoticationDismissTouchType:InAppNoticationDismissTouchType = InAppNoticationDismissTouchType.LeftAndRight):View.OnTouchListener {

    //移动的最小距离
    private var mSlop:Int = 0
    //最小滑动速度
    private var mMinFlingVelocity:Int = 0
    //动画时间
    private var mAnimationTime:Long = 0
    //控件的宽度
    private var mViewWidth = 0

    private var mDownX:Float = 0F
    private var mDownY:Float = 0F
    private var mSwiping: Boolean = false
    private var mSwipingSlop: Int = 0

    init {
        //获取view配置
        val vc = ViewConfiguration.get(mView.context)
        //获取触摸的最小距离
        mSlop = vc.scaledTouchSlop
        //获取最小滑动速度
        mMinFlingVelocity = vc.scaledMinimumFlingVelocity * 16
        //动画时间
        mAnimationTime = mView.context.resources.getInteger(android.R.integer.config_shortAnimTime).toLong()
    }

    override fun onTouch(view: View?, motionEvent: MotionEvent?): Boolean {
        mViewWidth = mView.width
        when(motionEvent?.actionMasked){
            MotionEvent.ACTION_DOWN ->{//按下
                //按下的x坐标
                mDownX = motionEvent.rawX
                //按下的y坐标
                mDownY = motionEvent.rawY
                return false
            }
            MotionEvent.ACTION_MOVE ->{
                val deltaX = motionEvent.rawX - mDownX
                val deltaY = motionEvent.rawY - mDownY
                if(mInAppNoticationDismissTouchType==InAppNoticationDismissTouchType.LeftAndRight){
                    if(abs(deltaX) > mSlop && abs(deltaY) < abs(deltaX) / 2){
                        //可以滑动
                        mSwiping = true
                        mSwipingSlop = if(deltaX > 0) mSlop else -mSlop
                        //不允许父控件拦截触摸事件
                        mView.parent.requestDisallowInterceptTouchEvent(true)
                    }
                }else{
                    if(abs(deltaY) > mSlop && deltaY < 0){
                        //可以滑动
                        mSwiping = true
                        mSwipingSlop = if(deltaX > 0) mSlop else -mSlop
                        //不允许父控件拦截触摸事件
                        mView.parent.requestDisallowInterceptTouchEvent(true)
                    }
                }


                if(mSwiping){
                    //设置控件移动
                    if(mInAppNoticationDismissTouchType==InAppNoticationDismissTouchType.LeftAndRight){
                        mView.translationX = deltaX
//设置控件透明度
                        mView.alpha = min(1f,1f-abs(deltaX) /mViewWidth)
                    }else{
                        if(deltaY<0){
                            mView.translationY = deltaY
                            //设置控件透明度
                            mView.alpha = min(1f,1f-abs(deltaY) /mView.height)
                        }

                    }

                    return true
                }
            }
            else ->{
                mView.performClick()
                return false
            }
        }
        return false
    }


}

enum class InAppNoticationDismissTouchType{
    LeftAndRight,//左边右边滑动关闭
    Top//上边滑动关闭
}